package com.IR.order.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.IR.order.service.BOrderBiz;
import com.IR.oss.util.MyDateProcessor;
import com.IR.shop.po.Orderdetail;
import com.IR.shop.po.Orders;
import com.IR.shop.po.Showorder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
public class BOrderAction {
	@Resource
	public BOrderBiz biz;
	private Orders orders;
	private Orderdetail orderdetail;
	private List<Orders> list1;
	private List<Orderdetail> list2;
	public Orders getOrders() {
		return orders;
	}
	public void setOrders(Orders orders) {
		this.orders = orders;
	}
	public Orderdetail getOrderdetail() {
		return orderdetail;
	}
	public void setOrderdetail(Orderdetail orderdetail) {
		this.orderdetail = orderdetail;
	}
	public List<Orders> getList1() {
		return list1;
	}
	public void setList1(List<Orders> list1) {
		this.list1 = list1;
	}
	public List<Orderdetail> getList2() {
		return list2;
	}
	public void setList2(List<Orderdetail> list2) {
		this.list2 = list2;
	}
	@RequestMapping(value="showOrderAll.do")
	public void showOrderAll() {
		list1=biz.showOrderAll();
		for(Orders o:list1) {
			list2=biz.showOrderId(o.getOrderid());
			System.out.println(list2.size());
		}
		System.out.println(list1.size());
	}
	@RequestMapping(value="showOrderPage.do")
	public void showOrderPage(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		int pagesize=new Integer(request.getParameter("pagesize"));
		int pagenow=new Integer(request.getParameter("pagenow"));
		list1=biz.showOrderPage(pagenow,pagesize);
		/*for(Orders o:list1) {
			list2=biz.showOrderId(o.getOrderid());
			System.out.println(list2.size());
		}*/
		PrintWriter out = response.getWriter();
		MyDateProcessor mydate=new MyDateProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor("orderdate", mydate);
		String str=JSONArray.fromObject(list1,config).toString();
		out.println(str);
		System.out.println(list1.size());
	}
	@RequestMapping(value="showOrderPage2.do")
	public void showOrderPage2(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		Long userid=new Long(request.getParameter("userid"));
		List<Showorder> list=new ArrayList<Showorder>();
		list1=biz.showOrderPage2(userid);
		for(Orders o:list1) {
			Showorder show=new Showorder();
			show.setUserid(o.getUserid());
			show.setSuser(o.getSuser());
			show.setSsite(o.getSsite());
			show.setSphone(o.getSphone());
			show.setSiteid(o.getSiteid());
			show.setPayway(o.getPayway());
			show.setOrderstate(o.getOrderstate());
			show.setOrderprice(o.getOrderprice());
			show.setOrderid(o.getOrderid());
			show.setOrderdate(o.getOrderdate());
			list2=biz.showOrderId(o.getOrderid());
			show.setList1(list2);
			list.add(show);
			//System.out.println(list2.size());
		}
		/*for(Showorder or:list) {
			System.out.println(or);
		}*/
		
		PrintWriter out = response.getWriter();
		//MyDateProcessor mydate=new MyDateProcessor();
		MyDateProcessor mydate=new MyDateProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor("orderdate", mydate);
		String str=JSONArray.fromObject(list,config).toString();
		out.println(str);
		//System.out.println(list1.size());
	}
	@RequestMapping(value="showOrderId.do")
	public void showOrderId(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		Long orderid=new Long(request.getParameter("orderid"));
		Orders o=new Orders();
		o=biz.showOrderId2(orderid);
		//Long orderid=new Long(request.getParameter("orderid"));
		list2=biz.showOrderId(orderid);
		Showorder show=new Showorder();
		show.setUserid(o.getUserid());
		show.setSuser(o.getSuser());
		show.setSsite(o.getSsite());
		show.setSphone(o.getSphone());
		show.setSiteid(o.getSiteid());
		show.setPayway(o.getPayway());
		show.setOrderstate(o.getOrderstate());
		show.setOrderprice(o.getOrderprice());
		show.setOrderid(o.getOrderid());
		show.setOrderdate(o.getOrderdate());
		show.setList1(list2);
		System.out.println(show.getList1().size());
		PrintWriter out = response.getWriter();
		MyDateProcessor mydate=new MyDateProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor("orderdate", mydate);
		String str=JSONObject.fromObject(show,config).toString();
		out.println(str);
	}
	@RequestMapping(value="showOrderState.do")
	public void showOrderState(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		Long userid=new Long(request.getParameter("userid"));
		int orderstate=new Integer(request.getParameter("orderstate"));
		List<Showorder> list=new ArrayList<Showorder>();
		list1=biz.showOrderState(userid,orderstate);
		for(Orders o:list1) {
			Showorder show=new Showorder();
			show.setUserid(o.getUserid());
			show.setSuser(o.getSuser());
			show.setSsite(o.getSsite());
			show.setSphone(o.getSphone());
			show.setSiteid(o.getSiteid());
			show.setPayway(o.getPayway());
			show.setOrderstate(o.getOrderstate());
			show.setOrderprice(o.getOrderprice());
			show.setOrderid(o.getOrderid());
			show.setOrderdate(o.getOrderdate());
			list2=biz.showOrderId(o.getOrderid());
			show.setList1(list2);
			list.add(show);
		}
		PrintWriter out = response.getWriter();
		MyDateProcessor mydate=new MyDateProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor("orderdate", mydate);
		String str=JSONArray.fromObject(list,config).toString();
		out.println(str);
	}
	@RequestMapping(value="showOrderState2.do")
	public void showOrderState2(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		int pagesize=new Integer(request.getParameter("pagesize"));
		int pagenow=new Integer(request.getParameter("pagenow"));
		int orderstate=new Integer(request.getParameter("orderstate"));
		list1=biz.showOrderState2(orderstate, pagenow, pagesize);	
		PrintWriter out = response.getWriter();
		MyDateProcessor mydate=new MyDateProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor("orderdate", mydate);
		String str=JSONArray.fromObject(list1,config).toString();
		out.println(str);
	}
}
